define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pifa/order/index' + location.search,
                    add_url: 'pifa/order/add',
                    edit_url: 'pifa/order/edit',
                    del_url: 'pifa/order/del',
                    multi_url: 'pifa/order/multi',
                    import_url: 'pifa/order/import',
                    table: 'pifa_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'mendian.name', title: __('所属仓库'), operate: 'LIKE' },
                        
                        //   { field: 'inv.nickname', title: __('上级'), operate: 'LIKE' },
                        // { field: 'inv.avatar', title: __('上级头像'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'order_no', title: __('Order_no'), operate: 'LIKE' },
                        // {field: 'inv_id', title: __('Inv_id')},
                        // { field: 'user_id', title: __('User_id') },
                          { field: 'address.md_name', title: __('店铺名称'), operate: 'LIKE' },
                          { field: 'address.image', title: __('门头照片'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        { field: 'consignee', title: __('Consignee'), operate: 'LIKE' },
                        { field: 'phone', title: __('Phone'), operate: 'LIKE' },
                        { field: 'address.address', title: __('Address'), operate: 'LIKE',
                        formatter: function (value, row, index) {
                            // 假设你有 address.address 和 address.house_number 两个字段
                            const addr = row.address?.address || '';
                            const houseNo = row.address?.xiangxi || ''; // 你也可以换成你具体需要的字段
                            return addr + ' ' + houseNo;
                        }},
                        { field: 'price', title: __('Price'), operate: 'BETWEEN' },
                        { field: 'youhui', title: __('优惠金额'), operate: 'BETWEEN' },
                        // { field: 'fen_price', title: __('分佣金额'), operate: 'BETWEEN' },
                        // {field: 'fen_price', title: __('Fen_price'), operate:'BETWEEN'},
                        // {field: 'transactionid', title: __('Transactionid'), operate: 'LIKE'},
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // { field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // { field: 'expiretime', title: __('Expiretime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'paytime', title: __('Paytime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false },
                        // { field: 'refundtime', title: __('Refundtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'canceltime', title: __('Canceltime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'orderstate', title: __('Orderstate'), searchList: { "0": __('Orderstate 0'), "1": __('Orderstate 1'), "2": __('Orderstate 2'), "3": __('Orderstate 3'), "3": __('已完成'), "4": __('已取消') }, formatter: Table.api.formatter.normal },
                        // {field: 'fenstate', title: __('Fenstate'), searchList: {"0":__('Fenstate 0'),"1":__('Fenstate 1'),"2":__('Fenstate 2')}, formatter: Table.api.formatter.normal},
                        {field: 'shippingstate', title: __('Shippingstate'), searchList: {"3":__('三方退款中'),"4":__('三方退款中'),"0":__('Shippingstate 0'),"1":__('Shippingstate 1'),"2":__('Shippingstate 2')}, formatter: Table.api.formatter.normal},
                        { field: 'freight_status', title: __('发货状态'), searchList: { "0": __('未发货'), "1": __('已发货') }, formatter: Table.api.formatter.normal },
                        { field: 'paystate', title: __('Paystate'), searchList: { "0": __('Paystate 0'), "1": __('Paystate 1') }, formatter: Table.api.formatter.normal },
                        { field: 'sh_status', title: __('售后状态'), searchList: { "0": __('待申请'), "1": __('待处理'), "2": __('已处理'), "3": __('已拒绝') }, formatter: Table.api.formatter.normal },
                        { field: 'emo', title: __('Emo'), operate: 'LIKE' },
                        { field: 'user.nickname', title: __('下单人'), operate: 'LIKE' },
                        { field: 'user.avatar', title: __('下单人头像'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image },


                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            clickToSelect: false,
                            buttons: [{
                                    'name': 'player',
                                    'title': function(row) {
                                        return '派单';
                                    },
                                    'icon': 'fa ',
                                    'text': '派单',
                                    'classname': 'btn btn-xs btn-info btn-dialog',
                                    'url': 'pifa/order/paidan/ids/{ids}',
                                    'extend': 'data-area=\'["55%","55%"]\'',
                                    visible:function(row){
                                        if (row.orderstate == 1 && row.peisong_user_id == 0) {
                                            return true
                                        }else{
                                             return false
                                        }
                                    },
                                },{
                                    'name': 'player',
                                    'title': function(row) {
                                        return '售后申请';
                                    },
                                    'icon': 'fa ',
                                    'text': '售后申请',
                                    'classname': 'btn btn-xs btn-info btn-dialog',
                                    'url': 'pifa/order/refund/ids/{ids}',
                                    'extend': 'data-area=\'["95%","95%"]\'',
                                    visible:function(row){
                                        if (row.sh_status == 1) {
                                            return true
                                        }else{
                                             return false
                                        }
                                    },
                                },{
                                    'name': 'player',
                                    'title': function(row) {
                                        return '部分退款';
                                    },
                                    'icon': 'fa ',
                                    'text': '部分退款',
                                    'classname': 'btn btn-xs btn-info btn-dialog',
                                    'url': 'pifa/order/refund2/ids/{ids}',
                                    'extend': 'data-area=\'["95%","95%"]\'',
                                    visible:function(row){

                                      
                                        if (row.price >= row.re_price) {
                                            return true
                                        }else{
                                             return false
                                        }
                                    },
                                },{
                                    'name': 'player',
                                    'title': function(row) {
                                        return '订单详情';
                                    },
                                    'icon': 'fa ',
                                    'text': '订单详情',
                                    'classname': 'btn btn-xs btn-info btn-dialog',
                                    'url': 'pifa/order/detail/ids/{ids}',
                                    'extend': 'data-area=\'["95%","95%"]\'',
                                },
                              
                            ]
                        }

                    ]
                ]
            });


            $(document).on("click", ".btn-selected1", function () {

                let ids = Table.api.selectedids(table);

                var event = $(this).attr('event');
                var url = $(this).attr('data-url');
                url = url+'?ids='+ids;
                // Layer.alert(JSON.stringify(Table.api.selecteddata(table)));

                Fast.api.open(url, '批量打印', {
                        area:['60%','70%'],
                        callback(selectData) {
                            // let check_user_ids = '';
                            // selectData.map(function (item, k) {
                            //     check_user_ids += check_user_ids ? ',' + item : item;
                            // });
                            // $(that).parent().parent().children('input[name="row[user_ids]"]').val(check_user_ids);
                            // $(that).parent().parent().parent().parent().find('.i-look').show();
                        }
                    });


            });


            $(document).on("click", ".btn-selected2", function () {

                let ids = Table.api.selectedids(table);

                var event = $(this).attr('event');
                var url = $(this).attr('data-url');
                
                url = url+'?ids='+ids;
                // Layer.alert(JSON.stringify(Table.api.selecteddata(table)));
                window.open(url);
                Fast.api.open(url, '订单成本', {
                        area:['100%','100%'],
                        callback(selectData) {
                            // let check_user_ids = '';
                            // selectData.map(function (item, k) {
                            //     check_user_ids += check_user_ids ? ',' + item : item;
                            // });
                            // $(that).parent().parent().children('input[name="row[user_ids]"]').val(check_user_ids);
                            // $(that).parent().parent().parent().parent().find('.i-look').show();
                        }
                    });


            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function() {
            Controller.api.bindevent();
        },
        detail: function() {
            $("#send").on('click', function() {
                var sn = $("#c-express_company").val();
                var name = $("#c-express_no").val();
                if (sn == '' || name == '') {
                    layer.msg("请填写正确的快递信息");
                    return false;
                }
                $("#send-form").attr("action", "pifa/order/detail").submit();
            });
            Controller.api.bindevent();
        },

        gysexport: function() {


            $('.btn-confirm').on('click', function() {
                let start = $('input[name="s_time"]').val();
                let end = $('input[name="e_time"]').val();
                let gongyingshang_id = $('input[name="gongyingshang_id"]').val();

                if (!start || !end) {
                    Layer.msg('请选择时间范围');
                    return;
                }

                let url = Fast.api.fixurl('pifa/order/gysexportpage') +
                          '?start=' + start +
                          '&end=' + end +
                          '&gongyingshang_id=' + gongyingshang_id;

                // 打开新窗口显示结果
                window.open(url, '_blank');
            });


            Controller.api.bindevent();
        },




        edit: function() {
            Controller.api.bindevent();
        },
        refund: function() {
            Controller.api.bindevent();
        },
        refund2: function() {
            Controller.api.bindevent();
        },
        
        daying: function() {
            Controller.api.bindevent();
        },
        paidan: function() {
             $("#send").on('click', function() {
                var sn = $("#c-peisong_user_id").val();
              
                if (sn == '' ) {
                    layer.msg("请选择配送员");
                    return false;
                }
                $("#send-form").attr("action", "pifa/order/paidan").submit();
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});